<!--
จำเป็นต้องมีสำหรับ หน้า print
div => "head" ถูกแสดงด้านบนทุกหน้า
thead => "tableHead" หัวตารางที่จะถูกแสดงทุกหน้า
2 => "" หัวตารางที่จะถูกแสดงทุกหน้า
tr => "row[no]" แถวในตาราง, class => "row"
td => class => "rowTd" ใช้หาความสูงแดวตารางใน IE
tr => "emptyRow" แถวว่างท้ายตาราง
4 => "foot" แสดงที่ส่วนท้ายทุกหน้า
-->
<div id="divFirstHeader"></div>
<div class="header">
    <div class="title">
        <?php echo $this->element('print/title'); ?>
        <table>
            <tr>
                <td width="120px"><label>Customer No.</label></td>
                <td><?php echo $this->data['Customer']['cust_no']; ?></td>
            </tr>
            <tr>
                <td><label>Customer Name</label></td>
                <td><?php echo $this->data['Customer']['name']; ?></td>
            </tr>
        </table>
    </div>
    <div class="documentTitle">
        <div class="documentNote">DISPATCH NOTE</div>
        <table class="documentNo">
            <tr>
                <td><label>Dispatch no.</label></td>
                <td><?php echo $this->data['Dispatch']['dispatch_no']; ?></td>
            </tr>
        </table>
        <table>
            <tr>
                <td><label>Date</label> <?php echo $this->data['Dispatch']['dispatch_date']; ?></td>
                <td><label>Page</label></td>
                <td><span class="pageNo">1/1</span></td>
            </tr>
            <tr>
                <td width="120px"><label>Ref. by Quotation no.</label></td>
                <td colspan="2"><?php echo $this->data['Dispatch']['quotation_no']; ?></td>
            </tr>
            <tr>
                <td><label>Ref. by PR. No.</label></td>
                <td colspan="2"><?php echo $this->data['Dispatch']['pr_no']; ?></td>
            </tr>
            <tr>
                <td><label>CSD</label></td>
                <td colspan="2"><?php echo $this->data['Dispatch']['csd']; ?></td>
            </tr>
        </table>
    </div>
    <br />
    <table class="tableTitle">
        <tr>
            <td width="120px"><label>Customer Site</label></td>
            <td width="200px"><?php echo $this->data['Site']['name']; ?></td>
            <td width="124px"><label>From Warehouse</label></td>
            <td><?php echo !empty($this->data['Warehouse']['name'])? $this->data['Warehouse']['name']: 'N/A'; ?></td>
        </tr>
        <tr>
            <td><label>Delivered To</label></td>
            <td><?php echo $this->data['Dispatch']['deliveree']; ?></td>
            <td><label>To Warehouse</label></td>
            <td><?php echo $this->data['Dispatch']['to_warehouse']; ?></td>
        </tr>
    </table>
    <br />&nbsp;
</div>

<div class="main">
    <table id="dataTable" class="dataTable" cellspacing="0" cellpadding="0" >
        <thead id="tableHead">
            <tr align="center">
                <th width="35px">Item<br>ลำดับ</th>
                <th width="100px">Part Number<br>รหัสสินค้า</th>
                <th>Description<br>รายการ</th>
                <th width="45px">Qty<br>จำนวน</th>
                <th width="45px">Unit<br>หน่วย</th>
                <th width="70px">Price/Unit<br>ราคา/หน่วย</th>
                <th width="70px">Amount<br>จำนวนเงิน</th>
                <th width="80px">Location<br>ตำแหน่ง</th>
            </tr>
        </thead>
        <tbody id="tableBody">
            <?php $i = 0; $total = 0; foreach($this->data['CustStockIn'] as $is) : ?>
                <?php
                $pl_count = count($is['ProductLocation']);
                for ($j = 0; $j < $pl_count; ++$j):
                ?>
            <tr id="row<?php echo $i; ?>" class="row">
                <td class="center rowTd"><?php echo $j == 0? ($i + 1): '&nbsp;'; ?></td>
                <td><?php echo $is['Product']['part_no']; ?></td>
                <td>
                    <?php echo !empty($is['CustProductSite']['text'])?
                            $is['CustProductSite']['text']:
                            $is['Product']['text']; ?>
                </td>
                <td class="text-right"><?php echo $is['ProductLocation'][$j]['deliver_qty']; ?></td>
                <td class="center"><?php echo $is['Product']['Unit']['title']; ?></td>
                <td class="text-right"><?php echo number_format($is['quotation_price'], 2); ?></td>
                <td class="text-right"><?php
                $amount = $is['quotation_price'] * $is['ProductLocation'][$j]['deliver_qty'];
                $total += $amount;
                echo number_format($amount, 2);
                ?></td>
                <td>
                        <?php echo !empty($is['ProductLocation'][$j]['Location']['name'])?
                        $is['ProductLocation'][$j]['Location']['name']:
                        'N/A'; // แสดง Location ถ้ามีข้อมูลอยู่?>
                </td>
            </tr>
            <?php
                // ถ้ามีแถวที่มี serial no หรือ other information อยู่ (ถ้าไม่มี จะมี แถวว่างมา 1 แถว)
                $k = 0;
                foreach($is['ProductLocation'][$j]['PS'] as $ps) :
                    if (!empty($ps['ProductSerial']['serial_no']) || !empty($ps['ProductSerial']['other_information'])) : ?>
                <tr class="product_serials_row">
                    <td>&nbsp;</td>
                    <td class="rowTd">
                        <span class="product_serial_no"><?php echo ++$k; ?>&nbsp;</span>
                        <span><?php echo !empty($ps['ProductSerial']['serial_no'])? $ps['ProductSerial']['serial_no']: 'N/A' ?></span>
                    </td>
                    <td>
                        <span><?php echo !empty($ps['ProductSerial']['other_information'])? $ps['ProductSerial']['other_information']: 'N/A' ?></span>
                    </td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <?php endif; endforeach; ?>
                <?php endfor; ++$i; ?>
            <?php endforeach; ?>
            <?php for ($i = 0; $i < 20; ++$i) : ?>
            <tr class="emptyRow"> <!-- สร้างแถวเปล่าขึ้นมาเพื่อยืดขนาดตารางให้เต็มหน้า-->
                <td class="rowTd">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
            </tr>
            <?php endfor; ?>
        </tbody>
        <tfoot>
            <tr>
                <td class="text-right" colspan="6">Total</td>
                <td class="text-right"><?php echo number_format($total, 2); ?></td>
                <td>&nbsp;</td>
            </tr>
        </tfoot>
    </table>
</div>
<br />
<div class="footer">
    <!-- print ให้ลูกค้าที่ไม่แสดง price adjustement -->
    <?php if ($this->data['Dispatch']['cust_see_price_adjustment'] == false) : ?>
    <table class="right">
        <tr>
            <td>
                <div class="custSignature">
                    <hr />
                    <span>Customer/Date</span>
                </div>
            </td>
            &nbsp;
            <td>
                <div class="custSignature">
                    <hr />
                    <span><?php echo $this->requestAction('system_preferences/get/company_name'); ?>/Date</span>
                </div>
            </td>
        </tr>
    </table>
    <?php else : ?>
    <table id="priceAdjustment" cellpadding="0" cellspacing="0">
        <tr>
            <th colspan="4" align="center">
                <h3>All item following by Price Adjustment Case <?php echo $this->data['Dispatch']['price_adjustment_case']; ?></h3>
            </th>
            <td rowspan="3" class="signature"><hr /><span>Customer/Date</span></td>
        </tr>
        <tr align="center">
            <th><label>Price Adjustment</label></th>
            <th><label>Date</label></th>
            <th><label>Price Adjustment</label></th>
            <th><label>Date</label></th>
        </tr>
        <tr align="center">
            <td align="left">Valid for quote price to</td>
            <td><?php echo $this->data['Dispatch']['quote_price_valid_to']; ?></td>
            <td align="left">2nd Price Adjustment on</td>
            <td><?php echo $this->data['Dispatch']['price_adjustment_2_date']; ?></td>
        </tr>
        <tr align="center">
            <td align="left">1st Price Adjustment on</td>
            <td><?php echo $this->data['Dispatch']['price_adjustment_1_date']; ?></td>
            <td align="left">Valid for 2nd Adjustment on</td>
            <td><?php echo $this->data['Dispatch']['price_adjustment_2_valid_to']; ?></td>
            <td rowspan="3" class="signature"><hr /><span><?php echo $this->requestAction('system_preferences/get/company_name'); ?>/Date</span></td>
        </tr>
        <tr align="center">
            <td align="left">Valid for 1st Adjustment to</td>
            <td><?php echo $this->data['Dispatch']['price_adjustment_1_valid_to']; ?></td>
            <td align="left">Customer must purchase on</td>
            <td><?php echo $this->data['Dispatch']['cust_purchase_date']; ?></td>
        </tr>
        <tr>
            <th colspan="4"><h2>*All item was delivered only for Consignment Stock</h2></th>
        </tr>
    </table>
    <?php endif; ?>
</div>
<div id="divLastFooter"></div>